/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.klikmee.sms.core.base.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import com.klikmee.sms.core.model.OutgoingMessage;
import com.klikmee.sms.core.model.Provider;
import com.klikmee.sms.core.model.Service;

/**
 * 
 * @author makcro
 */
public class OutgoingMessageDAO extends AbstractDAO<OutgoingMessage> implements OutgoingMessageFacadeLocal {

	public OutgoingMessageDAO() {
		super( OutgoingMessage.class );
	}

	public List<OutgoingMessage> findRange( int[] range ) {
		throw new UnsupportedOperationException( "Not supported yet." );
	}

	public int count() {
		throw new UnsupportedOperationException( "Not supported yet." );
	}

	public OutgoingMessage findByProvidersMessageIDAndProvider( String providersMessageID, String providerName ) {
		OutgoingMessage result;
		// try to find message 
		try {
			result = (OutgoingMessage) entityManager.createNamedQuery( "OutgoingMessage.findByProvidersMessageIdAndProvider" ).
					setParameter( "providersMesssageId", providersMessageID ).
					setParameter( "providerName", providerName ).
					getSingleResult();
		} catch (NoResultException e) {
			result = null;
		}

		return result;
	}

	public Long findNumberOfMessagesByProviderAndProvidersMessageId( Provider provider, String providersMessageID ) {
		Long result = (Long) this.entityManager.createNamedQuery( "OutgoingMessage.findNumberOfOutgoingMessageByProviderAndProvidersMessageID" ).
				setParameter( "providersMesssageId", providersMessageID ).
				setParameter( "providersMesssageId2", providersMessageID + "-%" ).
				setParameter( "provider", provider ).
				getSingleResult();
		return result;
	}

	public OutgoingMessage findByMessageIDAndProvider( Long messageID, String providerName ) {
		OutgoingMessage result;
		try {
			result = (OutgoingMessage) entityManager.createNamedQuery( "OutgoingMessage.findByMessageIdAndProvider" ).
					setParameter( "id", messageID ).
					setParameter( "providerName", providerName ).
					getSingleResult();
		} catch (NoResultException e) {
			result = null;
		}

		return result;
	}

	public List<OutgoingMessage> findMessagesForSending() {
		@SuppressWarnings("unchecked")
		List<OutgoingMessage> result = (List<OutgoingMessage>) entityManager.createNamedQuery( "OutgoingMessage.findMessagesForSending" ).
				setParameter( "createdState", new Integer( 24 ) ) // FREE MESSAGE
				.setParameter( "waitingToBeSentState", new Integer( 25 ) ) // BULK MESSAGE
				.setParameter( "paymentMessagesWaitingToBeSentState", new Integer( 34 ) ) // PAYMENT MESSAGE VIP & TELENOR
				.getResultList();

		return result;
	}

	public List<OutgoingMessage> findDeliveredByService( Service service ) {
		throw new UnsupportedOperationException( "Not supported yet." );
	}

	public List<OutgoingMessage> findDeliveredByServiceAndTime( Service service, Date time ) {
		throw new UnsupportedOperationException( "Not supported yet." );
	}

	public OutgoingMessage findByProvidesMessageId( String providersMessageId ) {
		try {
			return (OutgoingMessage) this.entityManager.createNamedQuery( "OutgoingMessage.findByProvidersMesssageId" )
					.setParameter( "providersMesssageId", providersMessageId ).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
}
